import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import Movie from './Movie/movie'  
import Cinema from './Cinema/cinema'
import Perform from './Perform/perform'
import Center from './Center/center'
import Detail from './Movie/detail'
import Search from './Search/search'
import Citylist from './Citylist/citylist'
import store from '../store'
const routes = [
  Movie,
  Cinema,
  Center,
  Perform,
  Detail,
  Search,
  Citylist,
  {
    path: '/*',
    redirect: '/movie'
  },

]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  //判断不是登录才验证
  if ('/center/login' != to.path && '/center/register' != to.path) {
    
    if (store.state.token) {
      next()
    } else {
      if (localStorage.getItem('token')) {
        store.dispatch('setToken', localStorage.getItem('token'))
        next()
        return
      }
      next('/center/login')
    }
  } else {
    next()
  }




})

router.beforeEach((to, from, next) => {
  const userInfo = store.state.userInfo
  const yzm = store.state.yzm
  if (!userInfo && to.meta.requireAuth && !yzm) {
    next({
      path: 'login'
      // query: {
      //   redirect: to.fullPath
      // }
    })
  } else {
    next()
  }
})

export default router
